﻿using System;
using BrainTechLLC.Silverlight.Controls;
using BrainTechLLC;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditAssociatedIDCollection : UserControl
    {
        public event EventHandler<ObjectEventArgsNonRef<long>> RequestEditID;

        public static readonly DependencyProperty AssociatedIDCollectionProperty = DependencyProperty.Register(
            "AssociatedIDCollection",
            typeof(AssociatedIDCollection),
            typeof(EditAssociatedIDCollection),
            new PropertyMetadata(null, new PropertyChangedCallback(AssociatedIDCollectionChanged)));

        public AssociatedIDCollection AssociatedIDCollection
        {
            get { return (AssociatedIDCollection)GetValue(AssociatedIDCollectionProperty); }
            set { SetValue(AssociatedIDCollectionProperty, value); }
        }

        private static void AssociatedIDCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            EditAssociatedIDCollection t = (EditAssociatedIDCollection)d;
            t.AssociatedIDCollection = e.NewValue as AssociatedIDCollection;
            t.RefreshData();
        }

        public void RefreshData()
        {
            // AssociatedIDCollection
            if (AssociatedIDCollection == null)
            {
                lstIDs.ItemsSource = null;
            }
            else
            {
                lstIDs.ItemsSource = AssociatedIDCollection.AllItems;
            }

            LayoutRoot.DataContext = AssociatedIDCollection;
        }

        public EditAssociatedIDCollection()
        {
            // Required to initialize variables
            InitializeComponent();
        }

        private void WrapPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            try
            {
                WrapPanel wrap = sender as WrapPanel;
                //if (wrap.ActualHeight > scrollViewer.ActualHeight - 14)
                //{
                //    wrap.Height = scrollViewer.ActualHeight - 14;
                //}
                //else if (wrap.ActualHeight == scrollViewer.ActualHeight - 14)
                //{
                //}
                //else if (!Double.IsNaN(wrap.Height))
                //{
                //    wrap.Height = double.NaN;
                //}
            }
            catch { }
        }

        private void EditAssociatedID_Loaded(object sender, RoutedEventArgs e)
        {
            FrameworkElement t = sender as FrameworkElement;
            t.StretchListBox();
        }

        private void EditAssociatedID_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            if (RequestEditID != null)
                RequestEditID(sender, e);
        }

    }
}